package com.sanpu.kxe.business.trade.view;

import android.content.Context;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.provider.ICardProvider;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.business.trade.contract.WithdrawContract;
import com.sanpu.kxe.business.trade.interfaces.IComfirmClickListener;
import com.sanpu.kxe.business.trade.presenter.WithdrawPresenter;
import com.sanpu.kxe.business.trade.provider.TradeOpenProvider;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.dialog.WithdrawComfirmDialog;
import com.sanpu.kxe.module.paypwd.popwindow.SelectPopupWindow;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.SoftInputUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Description：提现
 * Created by 薛勇军 on 2018/8/7.
 */

public class WithdrawActivity extends BaseTitleActivity implements WithdrawContract.View {

    @Bind(R.id.inputMoneyEt)
    EditTextWithDel inputMoneyEt;
    @Bind(R.id.bankIconIv)
    ImageView bankIconIv;
    @Bind(R.id.bankTitleTv)
    TextView bankTitleTv;
    @Bind(R.id.comfirmRtv)
    RoundTextView comfirmRtv;
    @Bind(R.id.select_bank)
    LinearLayout vSelectBank;
    @Bind(R.id.remarkTv)
    TextView remarkTv;

    private WithdrawContract.Presenter mPresenter;

    private SelectPopupWindow vMenuWindow;

    private List<BankInfoOutput> mBankInfoOutputs = new ArrayList<>();

    private WithdrawComfirmDialog vComfirmDialog;

    private BankInfoOutput mOutput;

    private String txRemark;

    private BigDecimal txFee;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_withdraw;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("收益提现");
        commonTitle.showRightTextView("提现详情");

        //打开输入密码的对话框
        vMenuWindow = new SelectPopupWindow(WithdrawActivity.this, new SelectPopupWindow.OnPopWindowClickListener() {
            @Override
            public void onPopWindowClickListener(final String psw, boolean complete) {
                if (psw.length() < 6) return;

                BankInfoOutput bankInfoOutput = (BankInfoOutput) bankTitleTv.getTag();
                if (bankInfoOutput != null) {
                    mPresenter.withdraw(inputMoneyEt.getText().toString(), psw, bankInfoOutput.getBankId());
                }
                vMenuWindow.clearPsw();
            }
        });
        String withdrawMoney = getIntent().getStringExtra(TradeConfig.INTENT_AMOUNT);
        mPresenter = new WithdrawPresenter(this);
        mPresenter.initViewData(withdrawMoney);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.getDepositBanks();
    }

    @Override
    protected void setListener() {
        super.setListener();
        inputMoneyEt.addTextChangedListener(mPresenter.getTextWatcher());
        vSelectBank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dialogNoTitletTwoBtnHaveCallBack(mContext,
                        "变更到账卡号须更改默认结算卡", "取消", "去更改",
                        new DialogViewListener() {
                            @Override
                            public void btnRightConfirm() {
                                ICardProvider provider = PublicProviderUtil.getProvider(ICardProvider.class);
                                if (provider != null) {
                                    provider.openMyBankListActivity(mContext);
                                }
                            }

                            @Override
                            public void btnLeftCancel() {

                            }
                        });
            }
        });
        comfirmRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SoftInputUtil.hideSoftInput(WithdrawActivity.this);
                mPresenter.onComfirmClick((BankInfoOutput) bankTitleTv.getTag(), inputMoneyEt.getText().toString());
            }
        });
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TradeOpenProvider.getInstance().openWithdrawListActivity(mContext);
            }
        });
    }

    @Override
    public void onRightCallBack() {
        vComfirmDialog = null;
        vComfirmDialog = new WithdrawComfirmDialog(
                mContext, mOutput, inputMoneyEt.getText().toString(), txFee, txRemark);
        vComfirmDialog.setListener(new IComfirmClickListener() {
            @Override
            public void onComfirmClick() {
                Rect rect = new Rect();
                getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
                int winHeight = getWindow().getDecorView().getHeight();
                vMenuWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, winHeight - rect.bottom);
            }
        });
    }

    @Override
    public void setInputMoney(String inputMoney) {
        if (!TextUtils.isEmpty(inputMoney)) {
            inputMoneyEt.setText(inputMoney);
        }
    }

    @Override
    public CharSequence getInputMoney() {
        return inputMoneyEt.getText();
    }

    /**
     * 显示选中的银行卡
     */
    private void showChooseDepositBank(BankInfoOutput output) {
        this.mOutput = output;
        if (output != null) {
            bankTitleTv.setTag(output);
            try {
                JKImageLoader.getInstance().displayImage(mContext,
                        UrlConstants.getBaseUrl() + output.getLogoPath(), bankIconIv);
                String bankNo = output.getBankNo();
                bankTitleTv.setText(output.getBankName().replace(" ", "") + "  (" +
                        bankNo.substring(bankNo.length() - 4, bankNo.length()) + ")");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void showDepositBanks(List<BankInfoOutput> outputs) {
        if (mBankInfoOutputs == null)
            mBankInfoOutputs = new ArrayList<>();
        mBankInfoOutputs.clear();
        if (outputs != null && outputs.size() > 0) {
            for (BankInfoOutput data : outputs) {
                mBankInfoOutputs.add(data);
                if (data.isDefStlFlg()) {
                    showChooseDepositBank(data);
                }
            }
            if (bankTitleTv.getTag() == null) {
                showChooseDepositBank(outputs.get(0));
            }
        }
    }

    @Override
    public void showTxRemarkView(String txRemark) {
        this.txRemark = txRemark;
        if (!TextUtils.isEmpty(txRemark)) {
            remarkTv.setText(txRemark);
        }
    }

    @Override
    public void showBalanceView(BigDecimal balance, BigDecimal txFee) {
        this.txFee = txFee;
        if (balance != null) {
            inputMoneyEt.setHint("可提现收益" + balance.toPlainString() + "");
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(WithdrawContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
